define(["jquery","bbcare","comm/datatable/datatable","comm/form/form","bch/app/classManager/classDetail.servers","bch/app/classManager/classDetail.ui"],function($,$bb,$datatable,$form,$servers,$ui){
	var event = {};

	/**
     * 新增班级成功后的回调函数
     */
    var ajaxEditClassCallback = function(data,arg){
        $("." + arg.className).modal("hide");
        $servers.ajaxClassInfo(arg.info.id,event.ajaxClassInfoCallBack);
    }

    /**
     * 点击“保存”
     */
    var onSubmit = function(){
        var data = $(this).data("data");
        var channelId = $bb.getChannelId();
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var queueName = $("#modal_queueName").val();
        var updateTime = $("#modal_updateTime").val();
        var queueIntro = $("#modal_queueIntro").val();
        var queueStatus = $("input[name=modal_queueStatus]:checked").val();
        var params = {
            "oprInfo":{
                "opCode":"1103",
                "opType":"2",
                "opNote":"【"+doctorName+"】医生修改【"+queueName+"】班级",
                "channelType":"2",
                "channelId":channelId,
            },
            "queueInfo":{
                "id":data.info.id,
                "queueName":queueName,
                "updateTime":updateTime,
                "queueIntro":queueIntro,
                "queueStatus":queueStatus
            },
        }
        $servers.ajaxEditClass(params,ajaxEditClassCallback,data);
    }

    /**
     * 取消修改班级
     */
    event.onCancel = function(){
        var className = $(this).data("className");
        $("." + className).modal("hide");
    }

	/**
	 * 修改班级信息
	 */
	var onEditClass = function(){
		var info = $(this).data("info");
		var className = "modal-editClass";
		$bb.modal({
			"className": className,
            "tit": "修改班级",
		},function(){
			$ui.editClass({
				"dom":$("." + className + " .modal-body"),
				"data":info
			});
			$("." + className).modal("show");
			$("#editClassSubmit").unbind("click");
            $("#editClassSubmit").data("data",{"className":className,"info":info});
            $("#editClassSubmit").click(onSubmit);
            $("#editClassCancel").unbind("click");
            $("#editClassCancel").data("className",className);
            $("#editClassCancel").click(event.onCancel);
		});
	}

	/**
	 * 请求班级详情成功后的回调函数
	 */
	event.ajaxClassInfoCallBack = function(data){
		$ui.info({
			"id":"classInfo",
			"data":data.queueInfo
		});
		$("#editClass").unbind("click");
		$("#editClass").data("info",data.queueInfo);
		$("#editClass").click(onEditClass);
	}

    /**
     * 修改患者任务内容信息成功后的回调函数
     */
    var ajaxSubmitTaskCallBack = function(data,params){
        $("." + params.className).modal("hide");
        $bb.toastShow("修改会员信息成功！");
        $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
        var args = $datatable.table.api().ajax.params();   //获取dt请求参数
    }

    /**
     * 点击保存  提交任务
     */
    var onSubmitTask = function(){
        if( $form.validate("classDetailModalContent") ){
            var params = $(this).data("params");
            var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
            var channelId = $bb.getChannelId();
            var contList = $form.getSaveData("classDetailModalContent");
            var userName = $bb.code.getValueByName("classDetailModalContent","userName");
            var params1 = {
                "oprInfo":{
                    "opCode":"1001",
                    "opType":"2",
                    "opNote":"【"+doctorName+"】医生对【"+userName+"】患者进行任务提交",
                    "channelType":"2",
                    "channelId":channelId,
                },
                "taskInfo":{
                    "ptTaskId":params.ptTaskId,
                    "contList":contList
                },
                "saveType":"1",
            };
            $servers.ajaxSubmitTask(params1,ajaxSubmitTaskCallBack,params);
        }
    }

    /**
     * 内容修改
     */
    var onEditMemberInfo = function(){
        var params = $(this).data("params");
        $ui.modalWrap({
            "dom":$("." + params.className + " .modal-body"),
            "flag":"1",
        });
        $form.init({
            "id":"classDetailModalContent",
            "questions":params.contList,
            "values":params.valueList,
        });
        // 内容修改保存
        $("." + params.className + " .classDetail-submit").unbind("click");
        $("." + params.className + " .classDetail-submit").data("params",params);
        $("." + params.className + " .classDetail-submit").click(onSubmitTask);
        // 取消
        $("." + params.className + " .classDetail-cancel").unbind("click");
        $("." + params.className + " .classDetail-cancel").data("className",params.className);
        $("." + params.className + " .classDetail-cancel").click(event.onCancel);
    }

    /**
     * 查询问卷成功后的回调函数
     */
    var ajaxContCallBack = function(data,params){
        $ui.modalWrap({
            "dom":$("." + params.className + " .modal-body"),
            "flag":"2",
        });
        $form.init({
            "id":"classDetailModalContent",
            "status":"readonly",
            "questions":data.contList,
            "values":data.valueList,
        });
        $("." + params.className).modal("show");
        $.extend(params,data);
        // 内容修改
        $("." + params.className + " .classDetail-submit").unbind("click");
        $("." + params.className + " .classDetail-submit").data("params",params);
        $("." + params.className + " .classDetail-submit").click(onEditMemberInfo);
        // 取消
        $("." + params.className + " .classDetail-cancel").unbind("click");
        $("." + params.className + " .classDetail-cancel").data("className",params.className);
        $("." + params.className + " .classDetail-cancel").click(event.onCancel);
    }

    /**
     * 患者临床路径节点下任务查询成功后的回调函数
     */
    var ajaxTasksCallBack = function(data,params){
        $.extend(params,{"ptTaskId":data.rows[0].id});
        $servers.ajaxCont(params,ajaxContCallBack);
    }

    /**
     * 查询临床路径下的节点成功后的回调函数
     */
    var ajaxUserCpNodesCallBack = function(data,params){
        $.extend(params,{"ptNodeId":data.rows[0].id});
        $servers.ajaxTasks(params,ajaxTasksCallBack);
    }

    /**
     * 查看个人信息
     */
   event.onInfoSearch = function(){
        var data = $(this).data("data");
        data.className = "modal-userInfo";
        $bb.modal({
            "tit":"会员详情",
            "className":data.className
        },function(){
            $servers.ajaxUserCpNodes(data,ajaxUserCpNodesCallBack);
        });
    }

    /**
     * 删除患者成功后的回调函数
     */
    var ajaxUserDeleteCallBack = function(data){
        $bb.toastShow("修改会员信息成功！");
        $datatable.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
        var args = $datatable.table.api().ajax.params();   //获取dt请求参数
    }

    var userDelete = function(params){
        $servers.ajaxUserDelete(params,ajaxUserDeleteCallBack);
    }

    /**
     * 删除患者
     */
    event.onDeleteUser = function(){
        var data = $(this).data("data");
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var channelId = $bb.getChannelId();
        var params = {
            "oprInfo":{
                "opCode":"1017",
                "opType":"2",
                "opNote":"【"+ doctorName +"】医生将【"+ data.userName +"】患者删除",
                "channelType":"2",
                "channelId":channelId,
            },
            "userInfo":{
                "userId":data.userId
            }
        };
        $bb.toastConfirmShow(userDelete,params,"确定删除该会员吗？");
    }

	return event;
});